package com.moli.iov.exception;

import com.moli.iov.rest.RestResponse;
import org.apache.dubbo.common.constants.CommonConstants;
import org.apache.dubbo.common.extension.Activate;
import org.apache.dubbo.rpc.Filter;
import org.apache.dubbo.rpc.Invocation;
import org.apache.dubbo.rpc.Invoker;
import org.apache.dubbo.rpc.Result;
import org.apache.dubbo.rpc.RpcException;

/**
 * <p>
 * dubbo 自定义异常filter
 * </p>
 *
 * @author zql
 * @since 2019/9/17 10:54
 */
@Activate(group = {CommonConstants.PROVIDER}, order = 9999)
public class BusinessExceptionDubboFilter implements Filter {

  @Override
  public Result invoke(Invoker<?> invoker, Invocation invocation) throws RpcException {
    Result result = invoker.invoke(invocation);
    if(result.hasException() && result.getException() instanceof BusinessException ){
      result.setValue(RestResponse.failure(result.getException().getMessage()));
      result.setException(null);
    }
    return result;
  }
}
